var areaJson = {1:"华东",2:"华南",3:"华北",4:"政企"};
var statusJson = {1:"已删除",0:"正常"};
var actType;//1：新增活动，2：编辑活动
var editId;
var checkFlag = true;
$(function() {
	initTable();
	doSearch();

	$('#addWin').myWindow({
		'width'  : 420,
		'height' : 520,
		'title'  : "新增/更新活动",
		'closed' :true
	});
	$('#showActivityBtn').click(function(){
		actType = 1;
		$('#addActivityBtn').html("新增");
		$('#addWin').myWindow('open');
		emptyEditWin();
		
	});
	$('#cancleAddActivityBtn').click(function(){
		$('#addWin').myWindow('close');
	});
	$("#addActivitySpan").click(addActivity);
});

function initTable(){
	$('#dataDrig').myTable({
		width : getWidth(0.98),
		rownumber : false,
		pagination : true,
		checkbox : false,
		paginationTop : true,
		url : '',
		pageNumberName:'pageNumber',
		pageSizeName:'pageSize',
		queryParams:{},
		pageList:[2,10,20,30],
		columns: [
					{field : 'topic',title : '活动名称',width : $(this).width() * 0.08,align : 'center',
						formatter : function(value, rec, index) {
							if(value.length>100){
								var html='<a href="javascript:void(0);" title="'+value+'">'+value.substr(0,100)+'...</a>';
								return html;
							}
							return value;
						}
					},
					{field : 'area',title : '区域',width : $(this).width() * 0.02,align : 'center',
						formatter : function(value, rec, index) {
							return areaJson[value]?areaJson[value]:"其它";
						}
					},
					{field : 'ctimeStr',title : '创建时间',width : $(this).width() * 0.02,align : 'center'},
					{field : 'remark',title : '备注',width : $(this).width() * 0.05,align : 'center',
						formatter : function(value, rec, index) {
							if(value || value == 0) {
								return value;
							}else {
								return "";
							}
					}},
					{field : 'status',title : '状态',width : $(this).width() * 0.02,align : 'center',
						formatter : function(value, rec, index) {
							return statusJson[value]?statusJson[value]:"其它";
						}
					},
					{field : 'aa',title : '操作',width : $(this).width() * 0.04,align : 'center',
						formatter : function(value, rec, index) {
							var html='';
							if(rec.status == 0){//显示按钮，数据是未删除状态
								html+='<a class="btn btn-primary btn-xs" onclick="openAddWin('+rec.id+');" href="javaScript:void(0)">编辑</a>&nbsp;&nbsp;';
								html+='<a class="btn btn-primary btn-xs" onclick="deleteAc('+rec.id+');" href="javaScript:void(0)">删除</a>';
							}
							return html;
						}
					}
				]
	});
}

//关闭试题
function closeQuestion(baseId,questionId){
	$.myMessager.confirm({'title':'温馨提示','msg':'确定删除试题？'},function(result){
		if(result){
            updateQuestionForDelete(baseId,questionId);
		}
	});
}

function openAddWin(id) {
	actType = 2;
	editId = id;
	var ac = getDataById("dataDrig", id);
	$('#area2').val(ac.area);
	$("#topic2").val(ac.topic);
	$("#remark2").val(ac.remark);
	$('#addActivityBtn').html("更新");
	$('#addWin').myWindow('open');
}

function updateAc() {
    $.myShadow.show('正在处理，请稍候.....');
    var parameter = {};
    parameter['topic'] = questionId;
    parameter['baseId'] = baseId;
    var url = '../activity/edit';
    $.post(
        url,
        parameter,
        function (result) {
            if (result.input) {
                $.myMessager.alert({'title': '提示', 'msg': ' <p><strong>' + result.input + '</strong></p>'});
            }
            doSearch();
            $.myShadow.remove();
        },
        'json'
    );
}

//关闭/开启
function deleteAc(id){
	$.myShadow.show('正在处理，请稍候.....');
	var parameter={};
	parameter['id']=id;
	var url='../activity/delete.action';
	$.post(
		url,
		parameter,
		function(result){
			if(result.input){
				$.myMessager.alert({'title':'提示','msg':' <p><strong>'+result.input+'</strong></p>'});
			}else if(result.result){
				$.myMessager.alert({'title':'提示','msg':' <p><strong>已删除</strong></p>'});
				doSearch();
			}
			$.myShadow.remove();
		},
		'json'
	);
}

//查询
function doSearch(){
	var queryParams ={};
	if(!checkParams()){
		return;
	}
	formJson=$("#searchForm").form2json();
	url='../activity/findPage';
	var opts = $('#dataDrig').myTable('options');
	opts.pageNumber = 1;
	queryParams = $.extend(queryParams,formJson);
	opts.queryParams = queryParams;
	opts.url=url;
	$('#dataDrig').myTable('options').url = url,
	$('#dataDrig').myTable('reload');
}

function emptyEditWin(){
	$('#area2').val('');
	$('#remark2').val('');
	$('#topic2').val('');
}
//新增试题
function addActivity(){
	if(!checkActivity()){
		return;
	}
	var parameter={};
	var area=$.trim($('#area2').val());
	var topic=$.trim($("#topic2").val());
	var remark=$.trim($("#remark2").val());
	parameter['area']=area;
	parameter['topic']=topic;
	parameter['remark']=remark;
	var url='';
	//新增试题
	if(actType==1){
		parameter['status']=0;
		url='../activity/add';
		$.post(
			url,
			parameter,
			function(result){
				if(result.info){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>'+result.input+'</strong></p>'});
				}else if(result.result){
					 $("#addWin").myWindow("close");
					emptyEditWin();
					$.myMessager.alert({'title':'提示','msg':' <p><strong>新增成功</strong></p>'});
					doSearch();
				}else{
					$.myMessager.alert({'title':'提示','msg':' <p><strong>未知错误！</strong></p>'});
				}
			},
			'json'
		);
	}else if(actType==2){//编辑试题
		url='../activity/edit';
		parameter['id']=editId;
		$.post(
			url,
			parameter,
			function(result){
				if(result.info){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>'+result.input+'</strong></p>'});
				}else if(result.result){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>更新成功</strong></p>'});
					 $("#addWin").myWindow("close");
					doSearch();
				}
			},
			'json'
		);
	}

}

function checkActivity(){
	var area=$.trim($('#area2').val());
	var topic=$.trim($("#topic2").val());
	var remark=$.trim($("#remark2").val());

	if(area==null||area==''){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>区域不能为空！</strong></p>'});
		return false;
	}
	if(topic==null||topic==''){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>活动名称不能为空！</strong></p>'});
		return false;
	}

	if(topic != null && topic.length>80){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>活动名称长度不能大于80！</strong></p>'});
		return false;
	}
	if(remark != null && remark.length>80){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>备注长度不能大于80！</strong></p>'});
		return false;
	}
	return true;
}

//去除输入框中的空格
function trimInput(jq){
	jq.val($.trim(jq.val()));
}

function doReset(){
	document.getElementById("searchForm").reset();
}

function onChangeDate() {
    if ($("#startTime").val() || $("#endTime").val()) {
        old_startTime = $("#startTime").val();
        old_endTime = $("#endTime").val();
    }
    if ($("#base_1").get(0).selectedIndex != 0) {
        document.getElementById("startTime").value = "";
        document.getElementById("endTime").value = "";
        checkFlag = false;
        document.getElementById("checkDateFlag").value = 1;
    } else {
        document.getElementById("startTime").value = old_startTime;
        document.getElementById("endTime").value = old_endTime;
        checkFlag = true;
        document.getElementById("checkDateFlag").value = 0;
    }
}

//跳转到明细页面
function toDetail(merchantId){
	document.getElementById("detailForm").reset();
	$('#merchantId_1').val(merchantId);
	$('#tabs').tabs('select',0);
	$('#tabs').tabs();
	doSearch(1);
}

function checkParams() {
    if(!checkFlag){
        return true;
    }
	if($("#startTime").val()==''||$("#endTime").val()=='') {
		$.myMessager.alert({'title':'提示','msg':' <p><strong>请填写完整开始、结束时间！</strong></p>'});
		return false;
	}
	if(dateDiff($("#startTime").val(),$("#endTime").val())<0) {
		$.myMessager.alert({'title':'提示','msg':' <p><strong>开始时间不能大于结束时间！</strong></p>'});
		return false;
	}
//	if(dateDiff($("#startTime").val(),$("#endTime").val())>31) {
//		$.myMessager.alert({'title':'提示','msg':' <p><strong>时间间隔不能超过一个月！</strong></p>'});
//		return false;
//	}
	return true;
}

//日期联动
function datePicker(type) {
	WdatePicker({
		skin : 'whyGreen',
		onpicked : function(dp) {
			var d = $dp.$DV($dp.cal.getNewDateStr(),
					{M:1}
			);
			var dd;
			if (d.M < 10) {
				if (d.d < 10) {
					dd = d.y + '-0' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-0' + d.M + '-' + d.d;
				}
			} else {
				if (d.d < 10) {
					dd = d.y + '-' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-' + d.M + '-' + d.d;
				}
			}
			$dp.$('endTime').value = dd;
			return true;
		}
	});
}

//日期联动
function datePicker() {
	WdatePicker({
		skin : 'whyGreen',
		onpicked : function(dp) {
			var d = $dp.$DV($dp.cal.getNewDateStr(),
					{M:1}
			);
			var dd;
			if (d.M < 10) {
				if (d.d < 10) {
					dd = d.y + '-0' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-0' + d.M + '-' + d.d;
				}
			} else {
				if (d.d < 10) {
					dd = d.y + '-' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-' + d.M + '-' + d.d;
				}
			}
			$dp.$('endTime').value = dd;
			return true;
		}
	});
}
